extends CharacterBody3D

@onready var camera_mount = $camera_mount
@onready var animation_player: AnimationPlayer = $visuals/mixamo_base/AnimationPlayer
@onready var visuals: Node3D = $visuals

var SPEED = 5.0
const JUMP_VELOCITY = 4.5

var walking_speed = 3.0
var running_speed = 5.0
var running = false;
var lock = false;

@export var sens_horizontal = 0.5
@export var sens_vertical = 0.5

func _ready():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	
func _input(event: InputEvent):
	if event is InputEventMouseMotion:
		rotate_y(deg_to_rad(-event.relative.x * sens_horizontal))
		visuals.rotate_y(deg_to_rad(event.relative.x*sens_horizontal))
		camera_mount.rotate_x(deg_to_rad(-event.relative.y * sens_vertical))

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
	if Input.is_action_pressed("run") and is_on_floor():
		SPEED = running_speed
		running = true
	else:
		SPEED = walking_speed
		running = false
	
	if Input.is_action_just_pressed("kick"):
		if animation_player.current_animation != "kick":
			animation_player.play("kick")
			lock = true
	
	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("left", "right", "forward", "backward")
	var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	
	if direction:
		if running == false && animation_player.current_animation != "walking":
			animation_player.play("walking")
			visuals.look_at(position+direction)
			
			velocity.x = direction.x * SPEED
			velocity.z = direction.z * SPEED
		
		elif  running && animation_player.current_animation != "running":
			animation_player.play("running")

			visuals.look_at(position+direction)
			
			velocity.x = direction.x * SPEED
			velocity.z = direction.z * SPEED
		
	else:
		if animation_player.current_animation != "idle":
			animation_player.play("idle")
			velocity.x = move_toward(velocity.x, 0, SPEED)
			velocity.z = move_toward(velocity.z, 0, SPEED)
	
	move_and_slide()
